<template>
  <div class="userInfo-wrapp">
    <div class="list-header">
      <el-button type="primary" size="mini" @click="add_user">添加用户</el-button>
    </div>
    <div class="user-table">
    <el-table
    size="small"
    :height="windowHeight"
    :data="tableData"
    style="width: 100%"
    :default-sort = "{prop: 'date', order: 'descending'}"
    border>
    <el-table-column
      prop="entrydate"
      label="入职日期"
      sortable
      :formatter="formatter"
      width="130">
    </el-table-column>

    <el-table-column
      prop="userid"
      label="用户工号"
      sortable>
    </el-table-column>

    <el-table-column
      label="姓名"
      prop="username">
    </el-table-column>

    <el-table-column
      prop="ismanager"
      label="是否管理者"
      width="100"
      :formatter="isManage">
    </el-table-column>

    <el-table-column
      prop="usertype"
      label="用户类型">
    </el-table-column>

    <el-table-column
      prop="workstate"
      label="在职状态"
      width="180">
    </el-table-column>

    <el-table-column
      prop="mobile"
      label="手机号码">
    </el-table-column>

    <el-table-column
      label="是否启用">
      <template slot-scope="scope">
        <el-switch v-model="scope.row.disenable"
        @change="SWChange($event,scope.row.id)"
        :active-value="1"
        :inactive-value="0"
        active-color="#13ce66">
       </el-switch>

     </template>
    </el-table-column>

    <el-table-column
      label="操作"
      width="180">
      <template slot-scope="scope">
       <el-button
         size="mini"
         @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
     </template>
    </el-table-column>

  </el-table>

   </div>
   <addUser-dialog
   v-if="dialogVisible"
   :dialogType="dialogType"
   :dialogVisible.sync="dialogVisible"
   :submitFn="submitFn"
   :submitFn2="update_submit"
   :PropData='userUpdateData'
   :orderData="roleInfoData"
   :companyInfo="companyInfo"
   :getUserRoleInfoList="getUserRoleInfoList">
 </addUser-dialog>
  </div>
</template>

<script>
import addUserDialog from './components/user_add'
  export default {
    data() {
      return {
        windowHeight: window.innerHeight -110,
        row:{},
        tableData: [],
        dialogVisible:false,
        userUpdateData:'',
        dialogType:'',
        statusItem:{
          '0':"禁用",
    			'1':"启用"
        },
        roleInfoData:[],
        uid:this.$store.getters.userUid,
        companyInfo:''
      }
    },
    methods: {
      SWChange(state,id){
        this.$post("/userInfo/updateUserInfoDisenable",[{disenable:state,id:id}],data => {
          this.$message({
             message: '设置成功！',
             type: 'success'
           });
          },error => {
            this.$message.error(error);
        });
      },
      isManage(row, column){
        return (row.ismanager)? "是":"否";
      },
      add_list(){

      },
      get_listInfo(){
        let params = {
            "pageNum": 0,
            "pageSize": 10,
            "companyid":this.$store.getters.companyId
        }
        this.$post("/userInfo/getUserInfoList",params,data => {
           console.log("用户信息查询接口数据",data.rows);
           this.tableData = data.rows;
          },error => {
            this.$message.error(error);
        });
      },
      add_user(){
        this.dialogType = "add";
        this.userUpdateData = {};
        this.dialogVisible = true;
        // this.getUserRoleInfoList(0);
      },
      submitFn(val){
        console.log(val);
        this.$post("/userInfo/savaUserInfo",val,data => {
          this.$message({
             message: '添加成功！',
             type: 'success'
           });
          this.dialogVisible = false;
          this.get_listInfo();
          },error => {
            this.$message.error(error);
        });
      },
      update_submit(val,saveRoleInfoParams){
        this.$post("/userInfo/updateUserInfo",val,data => {
          this.$post("/roleUser/saveUserRoleInfo",saveRoleInfoParams,data => {
            this.$message({
               message: '修改成功！',
               type: 'success'
             });
            this.dialogVisible = false;
            this.get_listInfo();
            },error => {
              this.$message.error(error);
          });
          },error => {
            this.$message.error(error);
        });
      },
      getUserRoleInfoList(uid,companyid){
       
        this.$post("/roleUser/getUserRoleInfoList",{id:uid,companyid:companyid},data => {
            this.roleInfoData = data;
             this.dialogVisible = true;
          },error => {
            this.$message.error(error);
        });
      },
      get_companyInfo(){
        this.$post("/company/getUserCompanyInfo",{uid:this.uid},data => {
             this.companyInfo = data;
          },error => {
            this.$message.error(error);
        });
      },
      handleEdit(scope,row){
        this.dialogType = "edit";
        let params={
          companyid:row.companyid,
          departmentid:row.departmentid,
          disenable:row.disenable,
          entrydate:row.entrydate,
          headfilepath:row.headfilepath,
          id:row.id,
          ismanager:row.ismanager,
          orderindex:row.orderindex,
          registerflag:row.registerflag,
          sex:row.sex,
          userid:row.userid,
          username:row.username,
          usertype:row.usertype,
          workstate:row.workstate,
          mobile:row.mobile,
          uid:row.uid
        }
        this.userUpdateData = params;

        this.getUserRoleInfoList(row.id,row.companyid);
      },
      formatter(row, column){
        let entrydate = row.entrydate.split(" ");
        return entrydate[0]
      }
    },
    components:{
      addUserDialog
    },
    created(){
      this.get_listInfo();
      this.get_companyInfo();
    }
  }
</script>
<style scoped lang="scss">
@import "./style.scss"
</style>